Crate redis_test

source ·
Expand description

Testing support

This module provides MockRedisConnection which implements ConnectionLike and can be used in the same place as any other type that behaves like a Redis connection. This is useful for writing unit tests without needing a Redis server.

§Example

use redis::{ConnectionLike, RedisError};
use redis_test::{MockCmd, MockRedisConnection};

fn my_exists<C: ConnectionLike>(conn: &mut C, key: &str) -> Result<bool, RedisError> {
    let exists: bool = redis::cmd("EXISTS").arg(key).query(conn)?;
    Ok(exists)
}

let mut mock_connection = MockRedisConnection::new(vec![
    MockCmd::new(redis::cmd("EXISTS").arg("foo"), Ok("1")),
]);

let result = my_exists(&mut mock_connection, "foo").unwrap();
assert_eq!(result, true);

Structs§

  • Represents a command to be executed against a MockConnection.
  • A mock Redis client for testing without a server. MockRedisConnection checks whether the client submits a specific sequence of commands and generates an error if it does not.

Traits§

  • Helper trait for converting redis::Cmd and redis::Pipeline instances into encoded byte vectors.
  • Helper trait for converting test values into a redis::Value returned from a MockRedisConnection. This is necessary because neither redis::types::ToRedisArgs nor redis::types::FromRedisValue performs the precise conversion needed.